package com.pengshun.modules.api.mapper;

import com.pengshun.modules.api.coal.service.dto.UserDto;
import com.pengshun.modules.api.order.service.dto.IncomeRecordDto;
import com.pengshun.modules.api.petrol.service.dto.JyPetrolStationEmployeeDto;
import com.pengshun.modules.api.transport.dto.CarNumberDto;
import com.pengshun.modules.api.user.service.dto.CarOwnerDto;
import com.pengshun.modules.api.user.service.dto.UserCenter;
import com.pengshun.modules.api.user.service.dto.UserDriverDto;
import com.pengshun.modules.domain.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

public interface PsUserInfoMapper {

    /**
     * 查询司机列表
     *
     * @param userId
     * @param status
     * @param page
     * @param size
     * @return
     */
    List<UserDriverDto> selectUserDriverList(@Param("userId") Long userId, @Param("status") Integer status, @Param("page") Integer page, @Param("size") Integer size);

    /**
     * 查询用户个人中心
     *
     * @param userType
     * @return
     */
    @Select("SELECT  uc.`user_center_id` userCenterId,uc.`name`,uc.`icon`,uc.`parent_id` parentId ,uc.is_login,uc.is_authentication FROM  " +
            "ps_user_center uc  " +
            "LEFT JOIN ps_user_center_type  uct ON uc.`user_center_id` =uct.`user_center_id`   " +
            "WHERE  uct.`type`=#{userType} " +
            "ORDER  BY uc.`parent_id`, uc.`sort`")
    List<UserCenter> selectUserCenterByType(@Param("userType") Integer userType);

    /**
     * 车辆列表
     *
     * @param userId
     * @param status
     * @param page
     * @param size
     * @return
     */
    List<CarOwnerDto> selectUserCarList(@Param("userId") Long userId, @Param("status") Integer status, @Param("page") Integer page, @Param("size") Integer size);

    /**
     * 车老板车辆列表
     *
     * @param userId
     * @return
     */
    @Select("SELECT  co.`car_owner_id` carOwnerId,co.`car_photo` carPhoto,co.`car_number` carNumber,co.`type_name` typeName," +
            "co.`car_wight` carWight,co.`status`,co.`comments`,co.`review_date` reviewDate  ,co.`apply_date`  applyDate,co.`car_load` carLoad " +
            "FROM `ps_car_owner`  co " +
            "WHERE co.`user_id`=#{userId} " +
            "AND co.`status`=2 AND co.`car_status`=1 " +
            "ORDER BY co.`review_date` DESC ")
    List<CarOwnerDto> carList(@Param("userId") Long userId);

    /**
     * 查询司机车主列表
     * @param id
     * @return
     */
    List<UserDriverDto> selectUserCarDriverList(@Param("id") Long id);

    User findUserByPhone(@Param("phone") String phone);

    User findUserById(@Param("id")Long id);

    User findUserByPhoneAndUserType(@Param("phone")String phone,@Param("userType") Integer userType);

    /**
     * 查询车辆 未绑定司机列表
     * @param carUserId
     * @param carDrivingIds
     * @param status
     * @return
     */
    List<UserDriverDto> selectNotBindingCarList(@Param("carUserId") Long carUserId, @Param("carDrivingIds") String carDrivingIds,
                                                @Param("status") Integer status,@Param("carOwnerId") Long carOwnerId);

    /**
     * 查询车辆绑定的司机 关联id
     * @param carOwnerId
     * @return
     */
    @Select("SELECT   GROUP_CONCAT(owner_driving_id)  FROM  ps_car_driving  WHERE car_owner_id =#{carOwnerId}")
    String selectCarDrivingIds(@Param("carOwnerId") Long carOwnerId);

    /**
     * 查询司机绑定的 车辆关联id
     * @param ownerDrivingId
     * @return
     */
    @Select("SELECT   GROUP_CONCAT(car_owner_id)  FROM  ps_car_driving  WHERE owner_driving_id =#{ownerDrivingId}")
    String selectCarOwnerIds(@Param("ownerDrivingId") Long ownerDrivingId);

    /**
     *查询司机未关联车辆
     * @param carUserId
     * @param carOwnerIds
     * @param status
     * @return
     */
    List<CarNumberDto> selectNotBindingDrivngList(@Param("carUserId") Long carUserId, @Param("carOwnerIds") String carOwnerIds, @Param("status") Integer status);

    List<UserDriverDto> selectUserBossList(@Param("userId") Long userId, @Param("status") Integer status, @Param("page") Integer page, @Param("size") Integer size);

    /**
     * 查询用户余额交易记录
     * @param status
     * @param page
     * @param size
     * @return
     */
    List<IncomeRecordDto> getWalletList(@Param("status") Integer status, @Param("page") Integer page, @Param("size") Integer size, @Param("userId") Long userId);

    /**
     * 查询用户流水
     * @param date
     * @param page
     * @param size
     * @param userId
     * @return
     */
    List<IncomeRecordDto> getRunningWaterList(@Param("date") Date date, @Param("page") Integer page, @Param("size") Integer size, @Param("userId") Long userId);

    /**
     *  查询运输中的货运
     * @param carOwnerId
     * @param drivingUserId
     * @return
     */
    @Select("SELECT  COUNT(1) FROM  ps_transport_take_carriage  WHERE car_owner_id=#{carOwnerId} and user_id=#{drivingUserId} AND `status` BETWEEN 1 AND 2")
    Integer findInTransportByCarnum(@Param("carOwnerId") Long carOwnerId, @Param("drivingUserId") Long drivingUserId);

    /**
     * 查询合同提示
     * @return
     */
    @Select("select * from ps_contract_hint where status=1")
    PsContractHint getHint();

    /**
     * 查询合同详情
     * @return
     */
    @Select("select * from ps_contract_detil where status=1")
    PsContractDetil getContractDetil();
    /**
     * 查询合同须知
     * @return
     */
    @Select("select * from ps_contract_know where status=1")
    PsContractKnow getContractKnow();

    /**
     * 首页轮播记录
     * @return
     */
    @Select("select user_id,phone,user_type from sys_user order by create_time desc  limit 0,10 ")
    List<UserDto> findRoolUsers();

    @Select("SELECT  su.`user_id`, su.`alias`,se.`role`,ps.`name`,ps.`logo` " +
            "FROM `jy_petrol_station_employee` se " +
            "INNER JOIN sys_user su ON su.`user_id`=se.`user_id` " +
            "INNER JOIN jy_petrol_station ps ON ps.`petrol_station_id`=se.`petrol_station_id` " +
            "WHERE  se.`petrol_station_id`=#{petrolStationId} AND se.`employee_status`=1 AND su.`enabled`=1")
    List<JyPetrolStationEmployeeDto> findByPetrolStationId(@Param("petrolStationId") Long petrolStationId);

    List<IncomeRecordDto> getBillList(Integer status, Integer page, Integer size, Long userId);

    @Select("SELECT  SUM( so.`station_num`) station_num ,SUM(so.`real_amount`) real_amount " +
            "FROM  jy_petrol_station_order so " +
            "WHERE so.`buy_user_id`=#{userId}")
    JyPetrolStationOrder accruingAmounts(@Param("userId") Long userId);
}
